package com.manager.kudian.system.web;


import com.manager.kudian.common.persistence.Page;
import com.manager.kudian.common.persistence.PropertyFilter;
import com.manager.kudian.common.utils.PropertiesLoader;
import com.manager.kudian.common.web.BaseController;
import com.manager.kudian.system.dao.*;
import com.manager.kudian.system.entity.*;
import com.manager.kudian.system.service.AccountStatementService;
import com.manager.kudian.system.service.MediaResService;
import com.manager.kudian.system.service.OrderInfoService;
import com.manager.kudian.system.vo.AccountStatementVO;
import com.manager.kudian.system.vo.OrderDeliveryVO;
import com.manager.kudian.system.vo.OrderInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 角色controller
 * @author ty
 * @date 2015年1月13日
 */
@Controller
@RequestMapping("business/accountstatement")
public class AccountStatementController extends BaseController{

	@Autowired
	private AccountStatementService accountStatementService;

	@Autowired
	private OrderInfoDao orderInfoDao;

	@Autowired
	private OrderInfoService orderInfoService;

	@Autowired
	private MediaResService mediaResService;

	@Autowired
	private AccountStatementHisDao accountStatementHisDao;

	@Autowired
	private AccountStatementOrdersDao accountStatementOrdersDao;

	@Autowired
	private AccountStatementDao accountStatementDao;

	@Autowired
	private SellerDao sellerDao;
	/**
	 * 默认页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "business/ashistory";
	}

	/**
	 * 获取字典json
	 */
	@RequestMapping(value="json",method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> historyList(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<AccountStatementHis> page = getPage(request);
			page.setOrderBy("asDay");
			page.setOrder("desc");
			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);

			page = accountStatementHisDao.findPage(page, filters);

			map.put("rows", page.getResult());
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = "asdetailoneday/{asday}", method = RequestMethod.GET)
	public String asdetailoneday(@PathVariable("asday") String asday, Model model) {

		model.addAttribute("asday", asday);
		return "business/asdetailoneday";
	}

	@RequestMapping(value = "aslistjson/{asday}", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object>  asdetaillistoneday(@PathVariable("asday") String asday, HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Page<AccountStatement> page = getPage(request);

			List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(request);

			page = this.accountStatementDao.findPage(page, filters);

			List<AccountStatementVO> info = new ArrayList<AccountStatementVO>();
			for (AccountStatement as : page.getResult()) {
				AccountStatementVO bean = new AccountStatementVO();
				BeanUtils.copyProperties(as, bean);

				Seller s = this.sellerDao.get(as.getSellerId());
				bean.setSellerName(s.getName());
				bean.setBankAccount(s.getBankAccount());
				info.add(bean);
			}

			map.put("rows", info);
			map.put("total", page.getTotalCount());
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = "pay/{asId}")
	@ResponseBody
	public String pay(@PathVariable("asId") Integer asId, HttpSession session) {
		try {
			accountStatementService.pay(asId);

		} catch (Exception e) {
			return "设置支付状态失败";
		}
		return "设置支付状态成功";
	}
}
